package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.All_Users;
import com.smart.entity.LoginUser;
import com.smart.entity.Work;
import com.smart.mapper.WorkMapper;
import com.smart.service.UserService;
import com.smart.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    WorkMapper workMapper;

    @Autowired
    UserService userService;

    @Override
    public Map<String, Object> WorkAll(Integer currentPage, Integer pageSize) {
        Page<Work> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Work> iPage = workMapper.selectPage(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Map<String, Object> WorkList(Integer currentPage, Integer pageSize) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();;
        queryWrapper.select("work_id", "work_user","work_type","work_degree", "work_describe", "work_time", "work_state");
        Page<Work> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Work> iPage = workMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Map<String, Object> WorkPending(Integer currentPage, Integer pageSize) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();;
        queryWrapper.select("work_id", "work_user","work_type","work_degree", "work_describe", "work_time", "work_state");
        queryWrapper.eq("work_state",1);
        Page<Work> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Work> iPage = workMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Map<String, Object> WorkProcessed(Integer currentPage, Integer pageSize) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();;
        queryWrapper.select("work_id", "work_user","work_type","work_degree", "work_describe", "work_time", "work_state");
        queryWrapper.eq("work_state",2);
        Page<Work> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Work> iPage = workMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Map<String, Object> WorkClosed(Integer currentPage, Integer pageSize) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();;
        queryWrapper.select("work_id", "work_user","work_type","work_degree", "work_describe", "work_time", "work_state");
        queryWrapper.eq("work_state",3);
        Page<Work> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Work> iPage = workMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Integer WorkDelete(Integer id) {
        int result = workMapper.deleteById(id);
        return result;
    }

    @Override
    public Work WorkFind(Integer id) {
        Work work = workMapper.selectById(id);
        return work;
    }

    @Override
    public Integer WorkEdit(Work work) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", work.getWorkId());
        int update = workMapper.update(work, queryWrapper);
        return update;
    }

    @Override
    public Work WorkChat(Integer id) {
        Work work = workMapper.selectById(id);
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        work.setWorkDescribe(loginUser.getUsers().getRealname());
        return work;
    }

    @Override
    public List<Work> UserWorkList(int tab) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryWrapper.eq("work_user", loginUser.getUsers().getUid());
        if(tab == 1) queryWrapper.eq("work_state","1");
        else if(tab == 2 ) queryWrapper.eq("work_state","2");
        queryWrapper.select("work_id","work_type","work_time","work_state");
        List<Work> works = workMapper.selectList(queryWrapper);
        return works;
    }

    @Override
    public Work UserWorkDetails(String workId) {
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryWrapper.eq("work_user", loginUser.getUsers().getUid());
        queryWrapper.eq("work_id", workId);
        queryWrapper.select("work_id","work_type","work_time","work_state","work_img","work_describe");
        Work work = workMapper.selectOne(queryWrapper);
        return work;
    }

    @Override
    public Integer UserWorkAdd(Work work) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        work.setWorkUser(loginUser.getUsers().getUid());
        work.setWorkState(1);
        work.setWorkTime(new Date());
        int insert = workMapper.insert(work);
        return insert;
    }
}
